﻿using System;
using System.IO;
using System.Runtime.Serialization;
using MBWebService.Core.Enums;
using MediaBrowser.Library.Entities;
using MediaBrowser.Library.ImageManagement;
using System.Drawing;
using System.Drawing.Imaging;

namespace MBWebService.Core.Classes
{
    [DataContract]
    public class PictureItem : BasicItem
    {
        public PictureItem(BaseItem item, ImageTypes type, int maxWidth, int maxHeight)
            : base(item)
        {
            ImageType = type;
            LibraryImage image = null;
            switch (type)
            {
                case ImageTypes.BackdropImage:
                    image = item.BackdropImage;
                    break;
                case ImageTypes.BannerImage:
                    image = item.BannerImage;
                    break;
                case ImageTypes.PrimaryImage:
                    image = item.PrimaryImage;
                    break;
                case ImageTypes.SecondaryImage:
                    image = item.SecondaryImage;
                    break;
                case ImageTypes.Unkown:
                    break;
            }

            if (image != null)
            {
                PictureStream = GetImageFromLibraryImage(image, type, maxHeight, maxWidth);
                FileExtension = Path.GetExtension(image.Path).Replace(".", string.Empty);
            }
        }

        private Stream GetImageFromLibraryImage(LibraryImage image, ImageTypes type, int maxHeight, int maxWidth)
        {
            Stream retVal = null;
            
            string imagePath = image.GetLocalImagePath();

            try
            {
                if (File.Exists(imagePath))
                {
                    Image retImage = ResizeImage(imagePath, type, maxWidth, maxHeight);

                    retVal = ToStream(retImage, ImageFormat.Png);
                }
            }
            catch (Exception)
            {
            }

            return retVal;
        }

        public Image ResizeImage(string origFile, ImageTypes type, int maxWidth, int maxHeight)
        {
            System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(origFile);
            // Ensure the generated thumbnail is not being used by rotating it 360 degrees
            fullSizeImg.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            fullSizeImg.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (maxWidth <= 0)
            {
                maxWidth = int.MaxValue;
            }

            if (maxHeight <= 0)
            {
                maxHeight = int.MaxValue;
            }

            Image retVal;

            if (fullSizeImg.Height > maxHeight || fullSizeImg.Width > maxWidth)
            {
                //resize stuff
                int newWidth, newHeight;
                if (fullSizeImg.Height >= maxHeight && fullSizeImg.Width < maxWidth)
                {
                    newHeight = maxHeight;
                    newWidth = fullSizeImg.Width * maxHeight / fullSizeImg.Height;
                }
                else if (fullSizeImg.Width >= maxWidth && fullSizeImg.Height < maxHeight)
                {
                    newWidth = maxWidth;
                    newHeight = fullSizeImg.Height * maxWidth / fullSizeImg.Width;
                }
                else if (fullSizeImg.Width > maxWidth && fullSizeImg.Height > maxHeight)
                {
                    if ((fullSizeImg.Height / fullSizeImg.Width) > 0.75)
                    {
                        newHeight = maxHeight;
                        newWidth = fullSizeImg.Width * maxHeight / fullSizeImg.Height;
                    }
                    else
                    {
                        newWidth = maxWidth;
                        newHeight = fullSizeImg.Height * maxWidth / fullSizeImg.Width;
                    }
                }
                else
                {
                    throw new Exception("Image resizing not handled properly");
                }

                retVal = fullSizeImg.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
            }
            else
            {
                retVal = fullSizeImg;
            }

            return retVal;
        }

        public static Stream ToStream(Image image, ImageFormat formaw)
        {
            var stream = new System.IO.MemoryStream();
            image.Save(stream, formaw);
            stream.Position = 0;
            return stream;
        }

        [DataMember]
        public Stream PictureStream { get; set; }

        [DataMember]
        public string FileExtension { get; set; }

        [DataMember]
        public ImageTypes ImageType { get; private set; }
    }
}
